//
//  LoginViewModel.m
//  baokaodaxue
//
//  Created by dangyangyang on 15/5/25.
//  Copyright (c) 2015年 xxx. All rights reserved.
//

#import "LoginViewModel.h"
#import <AFNetworking.h>
@implementation LoginViewModel

+ (void)loginWithUser:(NSString *)user
                 type:(NSString *)type
             password:(NSString *)password
              success:(void (^)())success
              failure:(void (^)(NSString *))failure{
    
    NSURL *url = [NSURL URLWithString:@"http://www.baokaodaxue.com/passport/login/ios"];
   
    NSString *post = [NSString stringWithFormat:@"user=%@&type=3&pwd=%@",user,password];
    
    NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
    
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    [request setHTTPMethod:@"POST"];
    [request setHTTPBody:postData];
    [request setTimeoutInterval:10.0];
    [request setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
    
    NSOperationQueue *queue = [[NSOperationQueue alloc]init];
    
    
    NSHTTPCookieStorage *cookieJar = [NSHTTPCookieStorage sharedHTTPCookieStorage];
    NSArray *cookieArray = [cookieJar cookies];
    for (id cookie in cookieArray) {
        [cookieJar deleteCookie:cookie];
        
    }
    

    [NSURLConnection sendAsynchronousRequest:request
                                       queue:queue
                           completionHandler:^(NSURLResponse *response, NSData *data, NSError *error){
                               if (error) {
                                   NSLog(@"Httperror:%@%ld", error.localizedDescription,(long)error.code);
                               }else{
                                   
                                                                     NSDictionary *responseDic=[NSJSONSerialization JSONObjectWithData:data
                                                                                             options:NSJSONReadingMutableLeaves
                                                                                                      error:nil];
                                   
                                   if ([[responseDic objectForKey:@"code"] isEqualToString:@"200"]) {
                                       
                                       NSHTTPCookieStorage *cookieJar = [NSHTTPCookieStorage sharedHTTPCookieStorage];
                                       NSArray *cookieArray = [cookieJar cookies];
                                       NSHTTPCookie *cookie = cookieArray[0];
                                       NSDictionary *dic = @{@"version":@(cookie.version),
                                                             @"name":cookie.name,
                                                             @"value":cookie.value,
                                                             };
                                       [[NSUserDefaults standardUserDefaults] setObject:dic forKey:@"Cookie"];
                                       
                                       
                                       success();
                                       
                                       
                                       
                                   }else{
                                    
                                       failure([responseDic objectForKey:@"message"]);
                                   }
                               }
                           }];
    
}
@end
